<?php
	/**
	 * @file
	 * @author Anthony Free
	 * @package fwork
	 * @subpackage utilities
	 * @version 20110930
	 */
	 /**
	  *
	  */

	/**
	 *
	 */
	define('PACIFY_MAXIMUM', 1);
	$gVAR = array();
	$pVAR = array();
	
	/**
	 * Class to auto instanciate all the $_GET and $_POST to $gVAR and $pVAR respectively
	 * @author Anthony Free
	 * @package fwork
	 * @subpackage utilities
	 * @version 20110930
	 */
	class autoPOSTGET extends Singleton {
		/**
		 * Constructor for class autoPOSTGET
		 * @since 20110930
		 * @global array $gVAR
		 * @global array $pVAR
		 */
		protected function SingletonConstruct() {
			global $gVAR; // SCOPE!
			global $pVAR;
			$pVAR = array();
			$gVAR = array();
			// Grab all the keys from $_GET and $_POST
			$getKeys = array_keys($_GET);
			$posKeys = array_keys($_POST);
			// Auto-Initialise all $gVAR and $pVAR
			foreach($getKeys as $arrayKey) {
				$this->safeGET($arrayKey);
			} $arrayKey = null;
			foreach($posKeys as $arrayKey) {
				$this->safePOST($arrayKey);
			} $arrayKey = null;
		}
		/**
		 * Destructor for class autoPOSTGET
		 * @since 20110930
		 */
		public function __destruct() {
			global $gVAR; // SCOPE!
			global $pVAR;
			unset($gVAR); unset($pVAR);
		}
		/**
		 * Initialise the $_GET to $gVAR Safely (aka, no blank keys)
		 * @param String $varName The Key for the $_GET Array
		 * @since 20110930
		 * @throws Exception
		 */
		private function safeGET($varName) {
			global $gVAR; // SCOPE!
			if(isset($_GET[$varName])) {
				// Okay we are initialised and said array_key exists
				$gVAR[$varName] = $_GET[$varName];
			} else {
				// This should never ever happen... if it does we should stop execution
				throw new SecurityException("Invalid array_keys, Hack Attempt?");
			}
		}
		/**
		 * Initialise the $_POST to $pVAR Safely (aka, no blank keys)
		 * @param String $varName The Key for the $_GET Array
		 * @return boolean True on Completion
		 * @since 20110930
		 * @throws Exception
		 */
		private function safePOST($varName) {
			global $pVAR; // SCOPE!
			if(isset($_POST[$varName])) {
				// Okay we are initialised and said array_key exists
				$pVAR[$varName] = $_POST[$varName];
			} else {
				// This should never ever happen... if it does we should stop execution
				throw new SecurityException("Invalid array_keys, Hack Attempt?");
			}
			return true;
		}
		/**
		 * Use this function via the array_key and the POST/GET to pacify it for output into the db
		 * @param String $arrayKey
		 * @param String $globalType
		 * @return boolean True on Completion
		 * @since 20110930
		 * @throws Exception
		 */
		public function PacifyForDatabase($arrayKey, $globalType) {
			$globalType = strtolower($globalType);
			if($globalType === "get") {
				global $gVAR;
				$localVar = &$gVAR; // Pass by Ref
			} elseif($globalType === "post") {
				global $pVAR;
				$localVar = &$pVAR; // Pass by Ref
			} else {
				throw new InvalidArgumentException("Invalid Type Specified for PacifyForDatabase");
			}
			if(isset($localVar[$arrayKey])) {
				$editingVariable = &$localVar[$arrayKey]; // Reference it for ease of code use
				$editingVariable = mysql_real_escape_string(/*htmlentities(*/$editingVariable/*, null, "utf-8")*/);
			} else {
				throw new InvalidArgumentException("Invalid array_key Specified for PacifyForDatabase");
			}
			return true;
		}
		/**
		 * Use this function via the array_key and the POST/GET to pacify it for output in html
		 * @param String $arrayKey
		 * @param String $globalType
		 * @return boolean True on Completion
		 * @since 20110930
		 * @throws Exception
		 */
		public function PacifyForDisplay($arrayKey, $globalType) {
			$globalType = strtolower($globalType);
			if($globalType === "get") {
				global $gVAR;
				$localVar = &$gVAR; // Pass by Ref
			} elseif($globalType === "post") {
				global $pVAR;
				$localVar = &$pVAR; // Pass by Ref
			} else {
				throw new InvalidArgumentException("Invalid Type Specified for PacifyForDisplay");
			}
			if(isset($localVar[$arrayKey])) {
				$editingVariable = &$localVar[$arrayKey]; // Reference it for ease of code use
				$editingVariable = htmlentities($editingVariable, ENT_NOQUOTES);
			} else {
				throw new InvalidArgumentException("Invalid array_key Specified for PacifyForDisplay");
			}
			return true;
		}
		/**
		 * Check all the given keys asked for exist
		 * @param array $arrayVars
		 * @param String $globalType
		 * @return boolean
		 */
		public function allExist($arrayVars, $globalType) {
			$globalType = strtolower($globalType);
			if($globalType === "get") {
				global $gVAR;
				$localVar = &$gVAR; // Pass by Ref
			} elseif($globalType === "post") {
				global $pVAR;
				$localVar = &$pVAR; // Pass by Ref
			} else {
				throw new InvalidArgumentException("Invalid Type Specified for allExist");
			}
			$allExist = true;
			foreach($arrayVars as $aKey) {
				if(!array_key_exists($aKey, $localVar)) {
					$allExist = false;
					break;
				}
			}
			return $allExist;
		}
		/**
		 * Pacify everything in gVAR and pVAR
		 * @param int $paranoia
		 */
		public function PacifyAllGlobals($paranoia) {
			global $gVAR;
			global $pVAR;
			if(!isset($gVAR)) {
				$gVAR = array();
			}
			if(!isset($pVAR)) {
				$pVAR = array();
			}
			if((count(array_diff($_GET, $gVAR)) != 0) || (count(array_diff($_POST, $pVAR)) != 0)) {
				$gVAR = array();
				$pVAR = array();
				$this->SingletonConstruct();
			}

			if(count($gVAR) != 0) {
				$keys_gVAR = array_keys($gVAR);
			} else {
				$keys_gVAR = false;
			}
			if(count($pVAR) != 0) {
				$keys_pVAR = array_keys($pVAR);
			} else {
				$keys_pVAR = false;
			}
			if($paranoia > 0) {
				$this->PacifyGlobal($gVAR, 'GET', $paranoia);
				$this->PacifyGlobal($pVAR, 'POST', $paranoia);
			} else {
				throw new InvalidArgumentException("Invalid Paranoia Level Specified");
			}
		}
		/**
		 * Private function to pacify a GET/POST array
		 * @param array $keysIn
		 * @param String $type
		 * @param int $paranoia
		 */
		private function PacifyGlobal($keysIn, $type, $paranoia) {
			if($paranoia > 0) {
				if($keysIn != false) {
					if($paranoia == 1) {
						$keys = array_keys($keysIn);
						foreach($keys as $key) {
							$this->PacifyForDisplay($key, $type);
						}
					} else {
						throw new InvalidArgumentException("Invalid Paranoia Level Specified");
					}
				}
			} else {
				throw new InvalidArgumentException("Invalid Paranoia Level Specified");
			}
		}
	}
$autoPOSTGET = Singleton::CreateInstance('autoPOSTGET');
?>